package com.estronger.shopmanage.ui.order.until;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.ArrayWheelAdapter;
import com.estronger.shopmanage.listener.OnWheelChangedListener;
import com.estronger.shopmanage.utils.DateUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.WheelView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Administrator on 2017-08-01.
 */

public class DateDialog {
    // private LinearLayout layout = null ;
    private static WheelView yearWV = null;
    private static WheelView monthWV = null;
    private static WheelView dayWV = null;
    private static WheelView hourWV = null;
    private static WheelView minuteWV = null;

    // 滚轮上的数据，字符串数组
    static String[] yearArrayString = null;
    static String[] dayArrayString = null;
    static String[] monthArrayString = null;
    static String[] hourArrayString = null;
    static String[] minuteArrayString = null;
    static Calendar c = null;
    static int year;
    static int month;
    static String currtTime;
    static int thisyear;
    static int thismonth;
    static int thisday;
    static int outOfrang = 0;
    public static void initView(View view) {
        // 获取当前系统时间
        c = Calendar.getInstance();
        currtTime = DateUtils.formatDate(c.getTime(),"yyyy-MM-dd");
        thisyear = Integer.parseInt(currtTime.substring(0,currtTime.indexOf("-")));
        thismonth = Integer.parseInt(currtTime.substring(currtTime.indexOf("-")+1,currtTime.lastIndexOf("-")));
        thisday = Integer.parseInt(currtTime.substring(currtTime.lastIndexOf("-")+1,currtTime.length()));
        yearWV = (WheelView) view.findViewById(R.id.time_year);
        monthWV = (WheelView) view.findViewById(R.id.time_month);
        dayWV = (WheelView) view.findViewById(R.id.time_day);
        hourWV = (WheelView) view.findViewById(R.id.time_hour);
        minuteWV = (WheelView) view.findViewById(R.id.time_minute);
        hourWV.setVisibility(View.GONE);
        minuteWV.setVisibility(View.GONE);
        yearArrayString = getYEARArray(2000, 100);
        monthArrayString = getDayArray(12);
        hourArrayString = getHMArray(24);
        minuteArrayString = getHMArray(60);
        // 设置每个滚轮的行数
        yearWV.setVisibleItems(5);
        monthWV.setVisibleItems(5);
        dayWV.setVisibleItems(5);
        hourWV.setVisibleItems(5);
        minuteWV.setVisibleItems(5);

        // 设置滚轮的标签
        yearWV.setLabel("年");
        monthWV.setLabel("月");
        dayWV.setLabel("日");
        hourWV.setLabel("时");
        minuteWV.setLabel("分");
        yearWV.setCyclic(true);
        monthWV.setCyclic(true);
        dayWV.setCyclic(true);
        hourWV.setCyclic(true);
        minuteWV.setCyclic(true);
        setData();
    }

    /**
     * 给滚轮提供数据
     */
    private static void setData() {
        // 给滚轮提供数据
        yearWV.setAdapter(new ArrayWheelAdapter<String>(yearArrayString));
        monthWV.setAdapter(new ArrayWheelAdapter<String>(monthArrayString));
        hourWV.setAdapter(new ArrayWheelAdapter<String>(hourArrayString));
        minuteWV.setAdapter(new ArrayWheelAdapter<String>(minuteArrayString));

        yearWV.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                // 获取年和月
                year = Integer.parseInt(yearArrayString[yearWV.getCurrentItem()]);
                month = Integer.parseInt(monthArrayString[monthWV
                        .getCurrentItem()]);
                // 根据年和月生成天数数组
                dayArrayString = getDayArray(getDay(year, month));
                // 给天数的滚轮设置数据
                dayWV.setAdapter(new ArrayWheelAdapter<String>(dayArrayString));
                // 防止数组越界
                if (dayWV.getCurrentItem() >= dayArrayString.length) {
                    dayWV.setCurrentItem(dayArrayString.length - 1);
                }
                // 显示的时间
                showDate();
            }
        });

        // 当月变化时显示的时间
        monthWV.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                // 获取年和月
                year = Integer.parseInt(yearArrayString[yearWV.getCurrentItem()]);
                month = Integer.parseInt(monthArrayString[monthWV
                        .getCurrentItem()]);
                // 根据年和月生成天数数组
                dayArrayString = getDayArray(getDay(year, month));
                // 给天数的滚轮设置数据
                dayWV.setAdapter(new ArrayWheelAdapter<String>(dayArrayString));
                // 防止数组越界
                if (dayWV.getCurrentItem() >= dayArrayString.length) {
                    dayWV.setCurrentItem(dayArrayString.length - 1);
                }
                // 显示的时间
                showDate();
            }
        });
    }

    // 设定初始时间
    static void setOriTime() {
        yearWV.setCurrentItem(getNumData(c.get(Calendar.YEAR) + "",
                yearArrayString));
        monthWV.setCurrentItem(getNumData(c.get(Calendar.MONTH) + 1 + "",
                monthArrayString) + 0);
        hourWV.setCurrentItem(getNumData(c.get(Calendar.HOUR_OF_DAY) + "",
                hourArrayString));
        minuteWV.setCurrentItem(getNumData(c.get(Calendar.MINUTE) + "",
                minuteArrayString));

        dayArrayString = getDayArray(getDay(year, month));
        dayWV.setAdapter(new ArrayWheelAdapter<String>(dayArrayString));
        dayWV.setCurrentItem(getNumData(c.get(Calendar.DAY_OF_MONTH) + "",
                dayArrayString));

        // 初始化显示的时间
        showDate();
    }

    // 设定时间
    static void setTime() {
        yearWV.setCurrentItem(getNumData(c.get(Calendar.YEAR) + "",
                yearArrayString));
        monthWV.setCurrentItem(getNumData(c.get(Calendar.MONTH) + 1 + "",
                monthArrayString) + 0);
        hourWV.setCurrentItem(getNumData(c.get(Calendar.HOUR_OF_DAY) + "",
                hourArrayString));
        minuteWV.setCurrentItem(getNumData(c.get(Calendar.MINUTE) + "",
                minuteArrayString));

        dayArrayString = getDayArray(getDay(year, month));
        dayWV.setAdapter(new ArrayWheelAdapter<String>(dayArrayString));
        dayWV.setCurrentItem(getNumData(c.get(Calendar.DAY_OF_MONTH) + "",
                dayArrayString));

        // 初始化显示的时间
        showDate();
    }

    // 显示时间
    static String showDate() {
        String selectDate = createDate(yearArrayString[yearWV.getCurrentItem()],
                monthArrayString[monthWV.getCurrentItem()],
                dayArrayString[dayWV.getCurrentItem()]);
        return selectDate;
    }


    // 生成时间
    static String createDate(String year, String month, String day) {
        String mon = DateUtils.fillZero(Integer.parseInt(month));
        String d = DateUtils.fillZero(Integer.parseInt(day));
//        String h = DateUtils.fillZero(Integer.parseInt(hour));
//        String m = DateUtils.fillZero(Integer.parseInt(minute));
        return year + "-" + mon + "-" + d;
    }

    // 在数组Array[]中找出字符串s的位置
    static int getNumData(String s, String[] Array) {
        int num = 0;
        for (int i = 0; i < Array.length; i++) {
            if (s.equals(Array[i])) {
                num = i;
                break;
            }
        }
        return num;
    }

    // 根据当前年份和月份判断这个月的天数
    public static int getDay(int year, int month) {
        int day;
        if (year % 4 == 0 && year % 100 != 0) { // 闰年
            if (month == 1 || month == 3 || month == 5 || month == 7
                    || month == 8 || month == 10 || month == 12) {
                day = 31;
            } else if (month == 2) {
                day = 29;
            } else {
                day = 30;
            }
        } else { // 平年
            if (month == 1 || month == 3 || month == 5 || month == 7
                    || month == 8 || month == 10 || month == 12) {
                day = 31;
            } else if (month == 2) {
                day = 28;
            } else {
                day = 30;
            }
        }
        return day;
    }

    // 根据数字生成一个字符串数组
    public static String[] getDayArray(int day) {
        String[] dayArr = new String[day];
        for (int i = 0; i < day; i++) {
            dayArr[i] = i + 1 + "";
        }
        return dayArr;
    }

    // 根据数字生成一个字符串数组
    public static String[] getHMArray(int day) {
        String[] dayArr = new String[day];
        for (int i = 0; i < day; i++) {
            dayArr[i] = i + "";
        }
        return dayArr;
    }

    // 根据初始值start和step得到一个字符数组，自start起至start+step-1
    public static String[] getYEARArray(int start, int step) {
        String[] dayArr = new String[step];
        for (int i = 0; i < step; i++) {
            dayArr[i] = start + i + "";
        }
        return dayArr;
    }

    public static Dialog yjanzPicker(final Activity activity, String title, final TextView date) {

        View view = LayoutInflater.from(activity).inflate(
                R.layout.picker_dialog, null);
        final Dialog dialog = new Dialog(activity, R.style.dialog);
        dialog.setContentView(view);
        initView(view);
        setOriTime();
        TextView tvfTitle = (TextView) view.findViewById(R.id.tvdialogtitle);
        tvfTitle.setText(title);
        final Button cancelButton = (Button) view.findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        Button okButton = (Button) view.findViewById(R.id.ok);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                 String selectDate = showDate();
                long oneDay =24*60*60*1000;
                if(StringToMill(selectDate)<(System.currentTimeMillis()-oneDay)){
                    selectDate = currtTime;
                    setOriTime();
                    Toast.show(activity,"预计安装日期须在录单日后的两年之内！",2000);
                }else if(StringToMill(selectDate)>(StringToMill(twoyearold())+oneDay)){
                    selectDate = twoyearold();
                    setOriTime();
                    Toast.show(activity,"预计安装日期须在录单日后的两年之内！",2000);
                }
                date.setText(selectDate);
                Tools.sYjazdate = showDate();
            }
        });

        return dialog;
    }
    public static String twoyearold(){
        int tyear = thisyear + 2;
        int tmonth = thismonth;
        int tday = thisday;
        if(thisday==29){
            if(thismonth==2){
                if(tyear % 4 == 0 && tyear % 100 != 0 || tyear % 400 == 0){
                    tday = 29;
                }else{
                    tday = 28;
                }
            }
        }else{
            tday = thisday;
        }
        return tyear + "-" + tmonth + "-" + tday;
    }

    public static long StringToMill(String dateTime){
        try {
            c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return c.getTimeInMillis();
    }
}
